/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session02;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import java.util.logging.Level;
import java.util.logging.Logger;
import jdbc.three_layer.entities.Classes;
import jdbc.three_layer.dal.DB_Util;

/**
 *
 * @author sinhnx <sinhnx@fpt.aptech.ac.vn>
 */
public class ClassesDAO {

    private Connection cn;

    public ClassesDAO() {
        cn = DB_Util.getConnection();
    }

    public int insert(Classes c) {
        try {
            if (cn != null) {
                CallableStatement cstm = cn.prepareCall("{?=call spInsertClasses(?,?)}");
                cstm.registerOutParameter(1, Types.INTEGER);
                cstm.setString(2, c.getClassName());
                cstm.setString(3, c.getLocation());

                //Thuc thi store procedure
                cstm.execute();
                return cstm.getInt(1);
            }else{
                System.out.println("CN NULL");
            }
        } catch (SQLException ex) {
            Logger.getLogger(ClassesDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 2;
    }
}
